package com.grapevine.contact;

import java.util.List;
import com.grapevine.contact.data.ContactData;
import com.grapevine.contact.data.ItemData;
import com.grapevine.contact.db.TypeManager;
import com.grapevine.contact.util.AndroidContact;
import com.grapevine.contact.util.ContactPhoto;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.CallLog;
import android.text.format.DateUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class DetialActivity extends Activity implements OnClickListener
{
	public static final String ID = "id";

	private ContactDetialAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		long id = getIntent().getLongExtra(ID, -1);
		ModeManager mode = (ModeManager) getIntent().getSerializableExtra(ModeManager.MODE);
		switchMode(id, mode);
	}

	private void switchMode(long id, ModeManager mode)
	{
		if (ModeManager.CONTACT_DETAIL == mode)
		{
			setContentView(R.layout.contact_detial);
			ContactData data = AndroidContact.getContacts(this, id);
			TextView name = (TextView) findViewById(R.id.contact_name);
			ImageView photo = (ImageView) findViewById(R.id.contact_photo);
			name.setText(data.getName());

			Bitmap bitmap = ContactPhoto.getPhoto(id, this);
			// Bitmap bitmap = null;
			if (null != bitmap)
			{
				photo.setImageBitmap(bitmap);
			}
			else
			{
				photo.setImageResource(R.drawable.ic_contact_picture);
			}
			ListView listView = (ListView) findViewById(R.id.contact_detial_list);

			mAdapter = new ContactDetialAdapter(data.getItemDatas());
			listView.setAdapter(mAdapter);
			listView.setOnItemClickListener(new OnItemClickListener()
			{
				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id)
				{
					ItemData data = (ItemData) mAdapter.getItem(position);
					Intent it = new Intent(Intent.ACTION_CALL, Uri.parse("tel: " + data.getValue()));
					startActivity(it);
				}
			});
		}
		else if (ModeManager.HISTORY_RECORD == mode)
		{
			setContentView(R.layout.calllog_detial_item);

			Cursor cursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null, CallLog.Calls._ID + " = " + id,
					null, null);

			if (cursor.moveToFirst())
			{
				int type = cursor.getInt(cursor.getColumnIndex(CallLog.Calls.TYPE));

				number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));

				long dateLong = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DATE));

				long timeValue = cursor.getLong(cursor.getColumnIndex(CallLog.Calls.DURATION));

				TextView stypeName = (TextView) findViewById(R.id.calllog_style_name);

				TextView date = (TextView) findViewById(R.id.calllog_date);

				TextView time = (TextView) findViewById(R.id.calllog_time);

				TextView disName = (TextView) findViewById(R.id.calllog_name);

				ImageView photoStyle = (ImageView) findViewById(R.id.calllog_photo_status);
				((TextView) findViewById(R.id.calllog_number)).setText(number);

				// Calendar calendar = Calendar.getInstance();
				// calendar.setTimeInMillis(dateLong);
				// int year = calendar.get(Calendar.YEAR);
				// int month = calendar.get(Calendar.MONTH) + 1;
				// int day = calendar.get(Calendar.DAY_OF_MONTH);
				// int week = calendar.get(Calendar.WEEK_OF_MONTH) + 1;
				//
				// int hour = calendar.get(Calendar.HOUR_OF_DAY);
				//
				// int minute = calendar.get(Calendar.MINUTE);
				// String dateValue = year + "年" + month + "月" + day + "日" +
				// hour + ":" + minute + " 星期"
				// + getNumberText(week);
				CharSequence dateClause = DateUtils.formatDateRange(this, dateLong, dateLong,
						DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_WEEKDAY
								| DateUtils.FORMAT_SHOW_YEAR);

				date.setText(dateClause);

				// long c = timeValue / 60;
				// long m = timeValue % 60;

				switch (type)
				{
				case CallLog.Calls.INCOMING_TYPE:
					stypeName.setText("来电");
					disName.setText("呼叫");
					photoStyle.setImageResource(R.drawable.ic_call_log_header_incoming_call);

					time.setText(formatDuration(timeValue));

					break;
				case CallLog.Calls.MISSED_TYPE:
					time.setText("");
					disName.setText("呼叫");
					stypeName.setText("未接电话");
					photoStyle.setImageResource(R.drawable.ic_call_log_header_missed_call);
					break;
				case CallLog.Calls.OUTGOING_TYPE:
					stypeName.setText("外拨电话");
					disName.setText("重拨");
					time.setText(formatDuration(timeValue));
					photoStyle.setImageResource(R.drawable.ic_call_log_header_outgoing_call);
					break;
				}
			}
			cursor.close();

			findViewById(R.id.calllog_call).setOnClickListener(this);
			findViewById(R.id.calllog_sms).setOnClickListener(this);

		}
	}

	String number = "";

	String getNumberText(int number)
	{

		String value = "";

		switch (number)
		{
		case 1:
			value = "一";
			break;

		case 2:
			value = "二";
			break;

		case 3:
			value = "三";
			break;

		case 4:
			value = "四";
			break;

		case 5:
			value = "五";
			break;

		case 6:
			value = "六";
			break;
		case 7:
			value = "日";
			break;
		default:
			break;
		}

		return value;
	}

	private class ContactDetialAdapter extends BaseAdapter
	{
		private List<ItemData> mList;

		private ContactDetialAdapter(List<ItemData> list)
		{
			mList = list;
		}

		@Override
		public int getCount()
		{
			if (null != mList)
			{
				return mList.size();
			}
			return 0;
		}

		@Override
		public Object getItem(int position)
		{
			return mList.get(position);
		}

		@Override
		public long getItemId(int position)
		{
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent)
		{
			if (null == convertView)
			{
				convertView = LayoutInflater.from(DetialActivity.this).inflate(R.layout.contact_detial_item, null);
			}

			Holder holder = (Holder) convertView.getTag();

			if (null == holder)
			{
				holder = new Holder();

				holder.name = (TextView) convertView.findViewById(R.id.contact_detial_item_name);
				holder.number = (TextView) convertView.findViewById(R.id.contact_detial_item_number);

				holder.sms = (LinearLayout) convertView.findViewById(R.id.contact_detial_item_sms);
				holder.call = (ImageView) convertView.findViewById(R.id.contact_detial_item_call);

				convertView.setTag(holder);
			}

			final ItemData data = mList.get(position);

			int type = data.getType();

			switch (type)
			{
			case TypeManager.PHONE_TYPE_CUSTOM:
				holder.name.setText("呼叫" + data.getLable());
				break;
			case TypeManager.PHONE_TYPE_HOME:
				holder.name.setText("呼叫住宅电话");
				break;
			case TypeManager.PHONE_TYPE_MOBILE:
				holder.name.setText("呼叫手机");
				break;

			case TypeManager.PHONE_TYPE_OTHER:
				holder.name.setText("呼叫");
				break;

			case TypeManager.PHONE_TYPE_WORK:
				holder.name.setText("呼叫单位");
				break;

			default:
				break;
			}

			holder.number.setText(data.getValue());
			holder.sms.setOnClickListener(new OnClickListener()
			{

				@Override
				public void onClick(View v)
				{
					Intent mmsintent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("smsto", data.getValue(), null));
					startActivity(mmsintent);
				}
			});

			holder.call.setOnClickListener(new OnClickListener()
			{

				@Override
				public void onClick(View v)
				{
					Intent it = new Intent(Intent.ACTION_CALL, Uri.parse("tel: " + data.getValue()));
					startActivity(it);
				}
			});

			return convertView;
		}

		private class Holder
		{
			private TextView name;

			private TextView number;

			private LinearLayout sms;

			private ImageView call;

		}
	}

	@Override
	public void onClick(View v)
	{
		Intent intent = new Intent();

		switch (v.getId())
		{
		case R.id.calllog_call:
			intent.setAction(Intent.ACTION_CALL);
			intent.setData(Uri.parse("tel:" + number));
			startActivity(intent);
			break;

		case R.id.calllog_sms:
			Intent mmsintent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts("smsto", number, null));
			startActivity(mmsintent);
			break;

		default:
			break;
		}

	}

	/**
	 * 格式化通话时间
	 * 
	 * @param elapsedSeconds
	 * @return
	 */
	private String formatDuration(long elapsedSeconds)
	{
		long minutes = 0;
		long seconds = 0;
		if (elapsedSeconds >= 60)
		{
			minutes = elapsedSeconds / 60;
			elapsedSeconds -= minutes * 60;
		}
		seconds = elapsedSeconds;
		return getString(R.string.callDetailsDurationFormat, minutes, seconds);
	}
}